//
//  ZDBExchangeOrderViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/17.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBExchangeOrderViewController.h"
#import "ZDBExchangeOrderCell.h"
#import "Order.h"
#import "ZDBExchangeModel.h"

@interface ZDBExchangeOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic,strong) NoNetworkView * workView;
@end

@implementation ZDBExchangeOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelsArry = [NSMutableArray array];
   

     [self.view addSubview:self.tableView];
    WeakSelf
    // 上拉加载
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf getOrder];
    }];
    [header beginRefreshing];
    self.tableView.mj_header = header;
    
}
- (void)placeholderViewWithFrame:(CGRect)frame NoNetwork:(BOOL)NoNetwork
{
    if (self.dataSource.count == 0)
    {
        [_workView removeFromSuperview];
        _workView = [[NoNetworkView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) NoNetwork:NoNetwork];
        [self.tableView addSubview:_workView];
        //_workView.centerY = self.collectionView.centerY-100;
    }else
    {
        [_workView dissmiss];
    }
    
}
/*加载订单*/
- (void)getOrder
{
    
    NSString  * inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"userGoods_getUserGoodsList"];
       WEAKSELF
       [[SmileHttpTool sharedInstance] POST :inurl parameters:nil origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
         STRONGSELFFor(weakSelf)
           
           if (statusCode==0) {
           //NSLog(@"userGoods_getUserGoodsList----%@",responseObject);
               NSDictionary * datadicc = [NSDictionary dictionaryWithDictionary:responseObject];
                          
                strongSelf.modelsArry = [ZDBExchangeModel arrayOfModelsFromDictionaries:[datadicc jsonArray:@"list"] error:nil];
           }else{
                [JMNotifyView showNotify:message isSuccessful:NO];
           }
             dispatch_async(dispatch_get_main_queue(), ^{
                                                    [self placeholderViewWithFrame:self.tableView.frame NoNetwork:NO];
                                                });
           // [strongSelf stopLoadingAnimation];
           [strongSelf.tableView reloadData];
           [strongSelf.tableView.mj_header endRefreshing];
       } failure:^(id responseObject, NSError *error) {
           NSLog(@"error----%@",error);
            STRONGSELFFor(weakSelf)
             [strongSelf.tableView.mj_header endRefreshing];
           [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
           // [self stopLoadingAnimation];
       }];
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}



#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - TABBAR_HEIGHT)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = CLEARCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0.0001)];
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBExchangeOrderCell" bundle:nil]  forCellReuseIdentifier:@"ZDBExchangeOrderCell"];
    
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelsArry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBExchangeModel * modell =self.modelsArry[indexPath.row];
    if ([modell.state integerValue]==1) {
        return 145;
    }
    return 195;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBExchangeOrderCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBExchangeOrderCell" forIndexPath:indexPath];
    
   cell.modell = self.modelsArry[indexPath.row];
    ZDBExchangeModel * modell =self.modelsArry[indexPath.row];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
  //  WEAKSELF
    [cell.duplicateButton add_BtnClickHandler:^(NSInteger tag) {
      //  STRONGSELFFor(weakSelf);
        
        [JMNotifyView showNotify:@"已复制到粘贴板" isSuccessful:YES];
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = modell.orderNumber;
    }];
    return cell;
}
@end
